#region Imported Namespaces
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;

using PHSRAG.Insight21.Context;
using PHSRAG.Insight21.SearchContext;
using Calendar = PHSRAG.WebControls.Calendar;
using DataAccess = PHSRAG.Utility.DataAccess;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
using PHSRAG.Insight21.InsightUtilities;
#endregion

namespace PHSRAG.Insight21.InsightWebControls
{
	/// <summary>
	/// The BudgetBuilderSearch control is used by BudgetBuilder summary page.
	/// </summary>
	[ToolboxData("<{0}:BudgetBuilderSearch runat=server />"), Designer(typeof(BudgetBuilderSearchDesigner))] 
	public class BudgetBuilderSearch : BaseSearch, INamingContainer, ISearch
	{
		#region Instance Variables
		private TextBox		folderNumber;
		private TextBox		originatorName;
        private DropDownList status;
	    private ICachePolicy cachePolicy;
		private ControlCollection	controlCollection;
		#endregion

		#region ISearch Members
		/// <summary>
		/// Reset the internal search state. This involves setting the value for all controls 
		/// to their default values and resetting the cached state of the search context.
		/// </summary>		
		public void Reset()
		{
			
		 ResetControls(controlCollection);
         BudgetBuilderSearchContext searchContext = (BudgetBuilderSearchContext)cachePolicy.GetCachedObject( BudgetBuilderSearchContext.CacheKey );
	     searchContext.Reset();

		}

		/// <summary>
		/// Get the search criteria. This is done by collecting the current values in all controls, 
		/// storing them in the BudgetBuilderSearchContext, and then returning the search criteria.
		/// </summary>
		public SearchCriteria SearchCriteria
		{			
	
			get
			{
              return ((BudgetBuilderSearchContext)cachePolicy.GetCachedObject( BudgetBuilderSearchContext.CacheKey )).SearchCriteria;
			}
		}

		/// <summary>
		/// Get the search criteria. This is done by collecting the current values in all controls, 
		/// storing them in the BudgetBuilderSearchContext, and then returning the search criteria.  We only 
		/// need to update the criterian for non-textbox controls b/c the Common search handles them
		/// </summary>
		public SearchCriteria UpdateSearchCriteria()
		{
			UserContext userContext = (UserContext)cachePolicy.GetCachedObject(UserContext.CacheKey);
			return GetSearchCriteria( userContext.DomainUserId, controlCollection);
		}

		#endregion
		
		#region Internal Methods
		/// <summary>
		/// This method is ONLY invoked by the associated designer to show the control contents at design time.
		/// </summary>
		internal void Design()
		{
			EnsureChildControls();
		}
		#endregion

		#region Overrides
		/// <summary>
		/// Create all controls. Notice that the creation is more than just instantiating the controls. 
		/// It also involves generating the underlying HTML so that the controls shows up per the need.
		/// </summary>
		protected override void CreateChildControls()
		{
			CreateControls();
			GenerateControlTree();
		}


		
		/// <summary>
		/// Initializes the cachepolicy. Create and cache a BudgetBuilderSearchContext object if necessary.
		/// Note that controls whose state must persist in the view state MUST be created BEFORE OnInit completes. We do
		/// this simply by invoking EnsureChildControls().
		/// </summary>
		/// <param name="e">Argument associated with the event</param>
		protected override void OnInit(EventArgs e)
		{
			cachePolicy = (Policy.ICachePolicy)Page.Session[Policy.Names.CachePolicyKey];
			BudgetBuilderSearchContext searchContext = cachePolicy.GetCachedObject(BudgetBuilderSearchContext.CacheKey) as BudgetBuilderSearchContext;
			if (searchContext == null)
			{
				searchContext = new BudgetBuilderSearchContext(cachePolicy);
				searchContext.Load(DataAccess.ConnectionString);
				searchContext.Cache();
			}

			EnsureChildControls();
			base.OnInit(e);
			searchContext.GetSearchCriteria(Page.Request.Url.ToString(), controlCollection);
		}
		/// <summary>
		/// Initialize the contents of all controls on first load. If the BudgetBuilderSearchContext is not cached yet, a
		/// new instance is created and cached.
		/// </summary>
		/// <param name="e">Argument associated with the event</param>
		protected override void OnLoad(EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				BudgetBuilderSearchContext searchContext = (BudgetBuilderSearchContext)cachePolicy.GetCachedObject( BudgetBuilderSearchContext.CacheKey );
				SetControlState(searchContext.SearchCriteria);
			}
		}


        /// <summary>
		/// Render the control. Note that we simply ensure that all child controls are created, 
		/// and then delegate the call back to the base class.
		/// </summary>
		/// <param name="writer">The writer to use for rendering</param>
		protected override void Render(HtmlTextWriter writer)
		{
			EnsureChildControls();
			base.Render(writer);
		}
		#endregion

		#region Private Methods
		/// <summary>
		/// Create all controls in the CommonSearch.
		/// </summary>
		private void CreateControls()
		{
		
            Controls.Clear();
            controlCollection = new ControlCollection(this);

            originatorName = new TextBox();
            originatorName.ID = "originatorName";
            originatorName.CssClass = "DataInput";
            originatorName.Attributes["Label"] = "Originator Name";
            controlCollection.Add(originatorName);

            folderNumber = new TextBox();
            folderNumber.ID = "folderNumber";
            folderNumber.CssClass = "DataInput";
            folderNumber.Attributes["Label"] = "Proposal Number";
            controlCollection.Add(folderNumber);

            status = new DropDownList();
            status.ID = "status";
            status.CssClass = "DataInput";
            status.Attributes["Label"] = "Budget Status";
            LoadDropDownList(status, "ClinicalTrialBudgetStatus");
            controlCollection.Add(status);
      }

		/// <summary>
		/// Setup the control tree to reflect the actual (and exact) layout of the controls within the ExpandedSearch.
		/// </summary>
		private void GenerateControlTree()
		{
            string searchClientId = ((Policy.ICachePolicy)Page.Session[Policy.Names.CachePolicyKey]).GetCachedObject(Constants.SearchButtonClientIDKey) as string;
            if (searchClientId == null)
                searchClientId = "expandingPanel_commonSearch_search";

			Table t1 = new Table();
			t1.CssClass = "Search";
			t1.CellSpacing = t1.CellPadding = 5;
            t1.Attributes.Add("onkeypress", "return ClickButton(event, '" + searchClientId + "')");
            Controls.Add(t1);

			TableRow t1r1 = new TableRow();
			t1.Rows.Add(t1r1);

            TableCell t1r1c1 = new TableCell();
            t1r1c1.Attributes["valign"] = "top";
            t1r1c1.Attributes["align"] = "right";
            t1r1c1.Text = "Budget Status:";
            t1r1.Cells.Add(t1r1c1);

            TableCell t1r1c2 = new TableCell();
            t1r1c2.Attributes["valign"] = "top";
            t1r1c2.Controls.Add(status);
            t1r1.Cells.Add(t1r1c2);

            TableCell t1r1c3 = new TableCell();
			t1r1c3.Attributes["valign"] = "top";
            t1r1c3.Attributes["align"] = "right";
            t1r1c3.Text = "Originator Name:";
            t1r1.Cells.Add(t1r1c3);

            TableCell t1r1c4 = new TableCell();
            t1r1c4.Attributes["valign"] = "top";
            t1r1c4.Controls.Add(originatorName);
            t1r1.Cells.Add(t1r1c4);
            
            TableCell t1r1c5 = new TableCell();
			t1r1c5.Attributes["valign"] = "top";
            t1r1c5.Attributes["align"] = "right";
            t1r1c5.Text = "Proposal #:";
            t1r1.Cells.Add(t1r1c5);

            TableCell t1r1c6 = new TableCell();
            t1r1c6.Attributes["valign"] = "top";
            t1r1c6.Controls.Add(folderNumber);
            t1r1.Cells.Add(t1r1c6);
        }
		#endregion
	}

	#region Class BudgetBuilderSearchDesigner
	/// <summary>
	/// Designer associated with the BudgetBuilderSearch control.
	/// </summary>
	/// <remarks>
	/// This class cannot be used directly.
	/// </remarks>
	public class BudgetBuilderSearchDesigner : ControlDesigner
	{
		#region Overrides
		/// <summary>
		/// Force the control to create its child controls for display at design time.
		/// </summary>
		/// <returns>HTML representation of the control</returns>
		public override string GetDesignTimeHtml()
		{
			((BudgetBuilderSearch)Component).Design();
			return base.GetDesignTimeHtml();
		}
		#endregion
	}

	#endregion
}